package com.zsrt.java.day20161227.zp.demo01;

import java.text.DecimalFormat;
import java.util.Arrays;

import org.junit.Test;

public class RedEnvelope02 {

	/**
	 * <发1元的红包 然后让10个人去抢>
	 * 红包总值 ：1元  tValue = 1;
	 * 红包份数 ：2份  partNum = 2;
	 * 将红包总值随机分在红包份数中： 
	 */
	
	@Test
	public void random(){
		
		String[] peoples = new String[10];
		peoples[0] ="马云";
		peoples[1] ="王健林";
		peoples[2] ="马化腾";
		peoples[3] ="丁磊";
		peoples[4] ="卢志强";
		peoples[5] ="许家印";
		peoples[6] ="李彦宏";
		peoples[7] ="严昊";
		peoples[8] ="雷军";
		peoples[9] ="宗庆后";
		
		double[] re =control(peoples);
		System.out.println(Arrays.toString(re));
		
		for(int i=0;i<re.length;i++){
			System.out.println("恭喜："+peoples[i]+" 抽到 "+re[i]+"元的红包！");
		}
		
	}
	
	public static double[] control(String[] peopleNum){
		
		double[] hb = new double[peopleNum.length];
		
		double sum = 0;
		
		for(int i=0;i<peopleNum.length-1;i++){
			double rMoney = Math.random()*0.3;
			DecimalFormat df = new DecimalFormat("######0.00");
			String fr = df.format(rMoney);
			double first = Double.valueOf(fr).doubleValue();
			hb[i] = first;
			sum = sum+hb[i];
		}
		
		double temp = (1-sum)/2;
		double ran = Math.random()*0.01;
		DecimalFormat df = new DecimalFormat("######0.00");
		String fr = df.format(ran);
		double fran = Double.valueOf(fr).doubleValue();
		double one = temp-fran;
		double two = temp+fran;
		hb[peopleNum.length-1] = one;
		hb[peopleNum.length-1] = two;
	    
		return hb;
    }
}
